package com.longcai.piaojia.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.MessageAdapter;
import com.longcai.piaojia.db.BaseTable;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.zcx.helper.bound.BoundView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener {

    public static final String MESSAGE_RECEIVER = "receiveMessageBoradcast";
    @BoundView(R.id.message_lv) private ListView message_lv;

    private List<BaseTable.BaseA> list = new ArrayList<>();
    private MessageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_message);

        setBackTrue();

        setTitleName(getString(R.string.message_title_text));

        setRightName(getString(R.string.message_title_right_text), R.color.white, this);

        message_lv.setAdapter(adapter = new MessageAdapter(context, list));

        registerReceiver(receiveMessageBoradcast, new IntentFilter(MESSAGE_RECEIVER));

        loadDate();

    }

    private void loadDate(){

        list.clear();

        try {
            list.addAll(BaseApplication.baseDB.baseTable.query(null, null, null, null, null, "time desc", null));
        } catch (Exception e) {
            e.printStackTrace();
        }

        adapter.notifyDataSetChanged();

    }

    BroadcastReceiver receiveMessageBoradcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            loadDate();

        }
    };

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.title_right_ll:

                new WarnningDialog(this, getString(R.string.warnning_clean_message)) {
                    @Override
                    protected void onConfirm() {

                        BaseApplication.baseDB.baseTable.deleteAll();

                        loadDate();
                    }
                }.show();

                break;

        }

    }
}
